package mvc.models.proxys
{
	import mvc.Notifications;
	import mvc.models.VOs.OperationListVO;
	
	import mx.collections.ArrayCollection;
	
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	import xl.net.HttpCall;
	import xl.utils.I18n;
	
	/**
	 * 用户信息代理
	 * @author XiLing
	 */
	public class ProxyAuth extends Proxy implements IProxy
	{
		private static var _instance:ProxyAuth;
		public static const NAME:String = "ProxyAuth";

		public function ProxyAuth(proxyName:String=null, data:Object=null)
		{
			super(proxyName, data);
			
			//controlsVOs = new DataProvider();
		}
		
		public static function getInstance():ProxyAuth
		{
			if(!_instance)
				_instance = new ProxyAuth();
			return _instance;
		}
		
		override public function getProxyName():String
		{
			return NAME;
		}
		
		public function getRoles():void
		{
			HttpCall.yiiCall("srbac","auth/roleList",function(_data:Object):void{
				if(_data && _data.hasOwnProperty('content')&&_data.content.length>0)
				{
					facade.sendNotification(Notifications.MEDIATOR_UPDATE_AUTH_ROLES,_data.content);
				}
			});				
		}
		
		public function getTasks($role:String):void
		{
			HttpCall.yiiCall("srbac","auth/taskList",function(_data:Object):void{
				if(_data && _data.hasOwnProperty('content')&&_data.content.length>0)
				{
					
				}
			},{role:$role});				
		}
		
		public function getAllItem():void
		{
			HttpCall.yiiCall("srbac","auth/allItem",function(_data:Object):void{
				if(_data && _data.hasOwnProperty('content')&&_data.content.length>0)
				{
					parseManageListVO(_data.content);
					
				}
			});			
		}
		
		public function getAll():void
		{
			HttpCall.yiiCall("srbac","auth/all",function(_data:Object):void{
				if(_data && _data.hasOwnProperty('content')&&_data.content.length>0)
				{
					parseManageListVO(_data.content);
					
				}
			});			
		}
		
		private function parseManageListVO($data:Array):void
		{
//			var manageListVO:Vector.<AuthManageListVO> = new Vector.<AuthManageListVO>();
//			var len:int = $data.length;
//			var children:Array = null;
//			for(var i:int=0;i<len;++i)
//			{
//				var vo:AuthManageListVO = new AuthManageListVO();
//				vo.label = vo.name = $data[i].parent;
//				children = $data[i].children;
//				
//				var len2:int = children.length;
//				var childrenVO:Vector.<AuthManageListVO> = new Vector.<AuthManageListVO>(len2);
//				for(var j:int=0;j<len2;++j)
//				{
//					var childVO:AuthManageListVO = new AuthManageListVO();
//					childVO.label = childVO.name = children[j];
//					childrenVO[j] = childVO;
//				}
//				vo.children = childrenVO;
//				manageListVO.push(vo);
//			}
//			sendNotification(Notifications.MEDIATOR_UPDATE_MAIN_DATA,manageListVO,ContentAuthManage.NAME);
		}
		
		public function getOperationList():void
		{
			HttpCall.yiiCall("srbac","auth/list",function(_data:Object):void{
				if(_data && _data.hasOwnProperty('content')&&_data.content.length>0)
				{
					sendNotification(Notifications.MEDIATOR_UPDATE_OPERATION_LIST,parseOperationListVO(_data.content));
				}
			});
		}
		
		/**
		 * 将控件栏数据转换成可用VO
		 * @param $data
		 */
		private function parseOperationListVO($data:Array):ArrayCollection
		{
			var data:ArrayCollection = new ArrayCollection();
			var len2:int=0;
			var pId:int=0;
			var children:Array=null;
			var len:int = $data.length;
			for(var i:int=0;i<len;++i)
			{
				var vo:OperationListVO = new OperationListVO();
				vo.id=pId+"";
				//vo.parentId = null;
				vo.name = $data[i].parent;
				vo.label = I18n.getLocaleStr(vo.name);
				vo.opened = true;
				data.addItem(vo);
				children = $data[i].children as Array;
				len2 = children.length;
				if(len2>0)
				{
					vo.children = new ArrayCollection();
					for (var j:int = 0; j < len2; ++j) 
					{
						var vo2:OperationListVO = new OperationListVO();
						vo2.parent = vo;
						vo2.name = children[j];
						vo2.label =  I18n.getLocaleStr(vo2.name);
						vo2.opened = true;
						vo2.id=pId+"_"+j;
						vo.children.addItem(vo2);
					}
				}
				else
				{			
				}
				++pId;
			}
			
			return data;
		}
	}
}